package com.hildeberto.versions.model;

import com.hildeberto.versions.model.svn.Commit;
import com.hildeberto.versions.model.svn.Entry;
import com.hildeberto.versions.repository.StatusRepo;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class CommitElement {

    public ObservableList<Element> getElements() {
        List<Entry> entries = StatusRepo.INSTANCE.listStatusEntries();
        List<Element> elements = new ArrayList<>();
        for (Entry entry: entries) {
            String status = entry.getWorkingCopyStatus().getItem();
            boolean included = true;
            if("unversioned".equals(status)) {
                included = false;
            }
            Commit commit = entry.getWorkingCopyStatus().getCommit();
            String date = null;
            if (commit != null) {
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
                date = sdf.format(commit.getDate().getTime());
            }
            Element element = new Element(included, entry.getPath(), status, date);
            elements.add(element);
        }
        return FXCollections.observableArrayList(elements);
    }

    public static class Element {
        private final SimpleBooleanProperty included;
        private final SimpleStringProperty path;
        private final SimpleStringProperty status;
        private final SimpleStringProperty date;

        private Element(boolean included, String path, String status, String date) {
            this.included = new SimpleBooleanProperty(included);
            this.path = new SimpleStringProperty(path);
            this.status = new SimpleStringProperty(status);
            this.date = new SimpleStringProperty(date);
        }

        public boolean getIncluded() {
            return this.included.get();
        }

        public void setIncluded(boolean included) {
            this.included.set(included);
        }

        public String getPath() {
            return this.path.get();
        }

        public void setPath(String path) {
            this.path.set(path);
        }

        public String getStatus() {
            return this.status.get();
        }

        public void setStatus(String status) {
            this.status.set(status);
        }

        public String getDate() {
            return this.date.get();
        }

        public void setDate(String date) {
            this.date.set(date);
        }

    }
}
